package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.*;
import com.ricohgag.entity.custom.ShopOrderGoodsVo;
import com.ricohgag.entity.custom.UserCartLogVo;
import com.ricohgag.mapper.ShopOrderGoodsMapper;
import com.ricohgag.service.GoodsCatalogService;
import com.ricohgag.service.GoodsService;
import com.ricohgag.service.ShopOrderGoodsService;
import com.ricohgag.service.ShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class ShopOrderGoodsServiceImpl extends ServiceImpl<ShopOrderGoodsMapper, ShopOrderGoods> implements ShopOrderGoodsService {
    @Autowired
    private ShopOrderGoodsMapper shopOrderGoodsMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCatalogService goodsCatalogService;
    @Autowired
    private ShopOrderService shopOrderService;

    @Override
    public int insertOrderGoods(String orderSn, VipUser vipUser, List<UserCartLogVo> userCartLogVos, BigDecimal moneySum) throws Exception{
        int count = 0;
        for(UserCartLogVo userCartLogVo:userCartLogVos){
            ShopOrderGoods shopOrderGoods = new ShopOrderGoods();
            Goods goods = userCartLogVo.getGoods();
            shopOrderGoods.setGoodsId(goods.getId());
            shopOrderGoods.setUserId(vipUser.getId());
            shopOrderGoods.setAddTime(LocalDateTime.now());
            shopOrderGoods.setGoodsName(goods.getGoodsName());
            shopOrderGoods.setGoodsImg(goods.getGoodsImg());
            shopOrderGoods.setPrice(goods.getSellPrice());
            shopOrderGoods.setGoNum(userCartLogVo.getUserCartLog().getGoNum());
            shopOrderGoods.setTotalPrice(goods.getSellPrice().multiply(new BigDecimal(userCartLogVo.getUserCartLog().getGoNum())));
            shopOrderGoods.setGoodsSn(goods.getGoodsSn());
            shopOrderGoods.setCatalogId(goods.getCatalogId());
            //给订单商品增加订单号
            ShopOrder shopOrder = shopOrderService.selectOrderBySn(orderSn);
            shopOrderGoods.setOrderId(shopOrder.getId());

            count += shopOrderGoodsMapper.insert(shopOrderGoods);

        }

        return count;
    }

    @Override
    public List<ShopOrderGoodsVo> selectGoodsByOrderId(int orderId) throws Exception{
        List<ShopOrderGoods> orderGoodsList = shopOrderGoodsMapper.selectList(new QueryWrapper<ShopOrderGoods>()
            .eq("order_id", orderId));

        //通过遍历订单商品，将商品和商品种类赋值给包装类
        List<ShopOrderGoodsVo> shopOrderGoodsVos = new ArrayList<>();
        for(ShopOrderGoods orderGoods:orderGoodsList){
            ShopOrderGoodsVo orderGoodsVo = new ShopOrderGoodsVo();
            orderGoodsVo.setShopOrderGoods(orderGoods);
            GoodsCatalog goodsCatalog = goodsCatalogService.selectCatalogOne(orderGoods.getCatalogId());
            System.err.println("orderGoods:"+orderGoods);
            orderGoodsVo.setGoodsCatalog(goodsCatalog);
            shopOrderGoodsVos.add(orderGoodsVo);
        }

        return shopOrderGoodsVos;
    }
}
